package com.qzdsoft.erpcloud.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.domain.sys.SysRole;
import com.qzdsoft.erpcloud.domain.sys.SysRoleTable;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysRoleService;
import com.qzdsoft.erpcloud.service.sys.SysRoleTableService;
import com.qzdsoft.erpcloud.service.sys.SysTableAuthService;
import com.qzdsoft.erpcloud.vo.sys.SysRoleQueryParam;
import com.qzdsoft.erpcloud.vo.sys.SysRoleTableEditInfo;
import com.qzdsoft.erpcloud.vo.sys.SysRoleTableQueryParam;
import com.qzdsoft.erpcloud.vo.sys.ZtreeNode;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.sys.SysRoleStatus;

/**
 * Created by Admin
 * 员工权限
 */
@Controller
@RequestMapping("/sys/role/table")
public class SysRoleTableController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(SysRoleTableController.class);
    @Autowired
    private SysRoleTableService sysRoleTableService;
    
    @Autowired
    private SysRoleService sysRoleService;
    
    @Autowired
    private SysTableAuthService sysTableAuthService;

//    @Autowired
//    private RedisHelper redisHelper;
    @Autowired
    private CenterCompanyService actCenterCompanyService;
    /**
     *
     * @param request
     * @param model
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(Model model) {
//    	sysRoleService.getAllRoleList();
//    	model.addAttribute("roleId", roleId);
//    	model.addAttribute("sysRoleMap", sysRoleService.getAllRoleList());
    	LoginUser user = getUser();
    	logger.debug("所属公司={}",user.getCompanyId());
    	model.addAttribute("user", user.getCompanyId());
    	List<TypeInfo> comInfo = user.getCompanys();
    	logger.debug("公司列表={}",comInfo.size());
        model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
//        model.addAttribute("sysRoleMap", redisHelper.getSysRoleMap());
//        model.addAttribute("sysTableAuthMap", sysTableAuthService.getAllTableAuthList());
        return "sys/role_table/index";
//        return "sys/role_table/index";
    }
    
    
    /**
    *初始查询
    * @param request
    * @param param
    * @return
    */
   @Permission(validate=false)
   @ResponseBody
   @RequestMapping("/data")
   public LayTableResponse data(HttpServletRequest request, SysRoleQueryParam info) {
//       Integer count = sysRoleTableService.selectDataCount(param.getRoleId(), param.getTableId());
//       List list = sysRoleTableService.selectData(param.getRoleId(), param.getTableId(), param.getiDisplayStart(), param.getiDisplayLength());
//       return new DataTablePo(count, list, param.getsEcho());
   	logger.debug("员工权限:companyId={}",info.getCompanyId());
   	
//   	info.setCompanyId(getUser().getCompanyId());
   	if(info.isFirst()) {
   		return LayTableResponse.nullDataPage();
   	}
   	 return sysRoleService.selectAllData(info);
   }
    
    
    /**
     * 打开新增页面
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView addinit() {
    	ModelAndView mv = new ModelAndView();
    	//用户当前的公司
    	mv.addObject("companyId",getUser().getCompanyId());
    	//拥有权限的公司
    	mv.addObject("actCenterCompanyMap", getUser().getCompanys());
    	mv.addObject("commonStatus2List", SysRoleStatus.ALL);
    	mv.setViewName("/sys/role_table/add");
    	return mv;
    }
    
    /**
     * 执行新增保存
     * @param request
     * @param sysRole
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="员工权限新增保存")
    @ResponseBody
    @RequestMapping("/add")
    public Object add(SysRole sysRole) {
        LoginUser user = getUser();
//        sysRole.setAllFunction(0);
//        sysRole.setAllData(0);
        sysRole.setType(1);//客户
        return sysRoleService.saveOrUpd(sysRole);
    }
    
    
    /**
     * 编辑数据
     * @param request
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/edit")
    public Object edit(@RequestParam("id") Integer id) {
    	logger.debug("员工权限修改id={}",id);
    	ModelAndView mv = new ModelAndView();
    	//拥有权限的公司
    	mv.addObject("actCenterCompanyMap", getUser().getCompanys());
    	mv.addObject("commonStatus2List", SysRoleStatus.ALL);
    	mv.addObject("obj",sysRoleService.selectById(id));
    	mv.setViewName("sys/role_table/edit");
    	return mv;
    }


    /**
     * 执行编辑
     * @param request
     * @param sysRole
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="员工权限修改保存")
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Object editExecute(SysRole sysRole) {
        LoginUser user = getUser();
        return sysRoleService.saveOrUpd(sysRole);
    }

    /**
     * 删除
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="员工权限删除")
    @ResponseBody
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return sysRoleTableService.del(id);
    }
    
    
    
    
    
    @Permission(validate=false)
    @RequestMapping("/table")
    public Object table(Model model,Integer roleId) {
//    	sysRoleService.getAllRoleList();
    	model.addAttribute("roleId", roleId);
        return "sys/role_table/table";
    }
    
    
    
    /**
     * 查询所有数据表
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/findAlltable")
    public Response<ZtreeNode> findAlltable() {
    	
    	return sysRoleTableService.findAllTable();
    }
    
    /**
     * 保存roleid和字段
     */
    @Permission(validate=false)
    @SysLog(description="员工权限设置表格权限修改保存")
    @ResponseBody
    @RequestMapping("/saveField")
    public Response saveRoleTableField(@RequestBody SysRoleTableEditInfo info) {
    	logger.debug("ids={},roleId={}",info.getIds(),info.getRoleId());
    	try{
    		return sysRoleTableService.saveRoleTableField(info);
    	}catch(Exception e){
    		ExceptionUtil.getStackTrace(e);
    		return new Response(ResultEnum.ERROR);
    	}
    }
    
    
    /**
     * 打开ztree页面
     * @param Id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/openTree")
    public String opendTree(Model model,Integer id){
    	model.addAttribute("roleId", id);
    	return "sys/role_table/treeNode";
    }

    /**
     * 菜单权限保存
     */
    @Permission(validate=false)
    @SysLog(description="员工权限设置修改保存")
    @ResponseBody
    @RequestMapping("/roleinit")
    public Response roleinit(Integer roleId,String id) {
    	logger.debug("权限保存:roleId={},id={}",roleId,id);
    	
    	return sysRoleService.roleInt(roleId, id);
    }

}
